package com.cfm880.webview;

import android.content.Context;
import android.support.annotation.NonNull;

import java.io.InputStream;
import java.util.Scanner;

/**
 * Created by cfm on 16-10-26.
 */

public class Utils {

    public static String getHtmlToString(Context context, int resId){
        InputStream inputStreamText = context.getResources().openRawResource(resId);

        // http://stackoverflow.com/questions/309424/read-convert-an-inputstream-to-a-string
        Scanner s = new Scanner(inputStreamText).useDelimiter("\\A");
        return s.hasNext() ? s.next() : "";
    }

    public static String removeTitleTags(String html){
        if (html.contains("<title>")){
            String neefRepalce = html.substring(
                    html.indexOf("<title>"),
                    html.indexOf("</title>")+8);
            String newHtml =  html.replace(neefRepalce, "");
            return newHtml;
        }
        return null;
    }

    public static String getTitle(String html){
        String title = html.substring(html.indexOf("<title>")+7, html.indexOf("</title>"));
        return title;
    }
}
